#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
import unittest

from Bio import SeqUtils

import task2


class TestTask2(unittest.TestCase):
    def setUp(self):
        test_filenames = ["gene1.fasta", "gene2.fasta", "gene3.gb"]
        self.files = [os.path.join('../../hometask-files/2-bio_toolkits', filename)
                      for filename in test_filenames]

    def test_calc_gc(self):
        records = task2.get_file_records(self.files[0])
        gc = task2.calc_gc(records[0])
        self.assertAlmostEqual(gc, SeqUtils.GC(records[0].seq))

    def test_get_file_format(self):
        self.assertEqual(task2.get_file_format('test.fasta'), 'fasta', msg='long fasta')
        self.assertEqual(task2.get_file_format('test.fa'), 'fasta', msg='short fasta')
        self.assertEqual(task2.get_file_format('test.gb'), 'genbank', msg='short genbank')
        self.assertEqual(task2.get_file_format('test.gbk'), 'genbank', msg='long genbank')
        self.assertRaises(Exception, task2.get_file_format, 'test.error', msg='unknown format')

    def test_load_records(self):
        records = task2.load_records(self.files)
        self.assertEqual(len(records), 3)

    def test_is_valid_file(self):
        for filename in self.files:
            if os.path.exists(filename) and os.path.isfile(filename):
                self.assertTrue(task2.is_valid_file(filename), msg='valid files')
        self.assertFalse(task2.is_valid_file('../'), msg='directory')
        self.assertFalse(task2.is_valid_file('unknown_file.removed'), msg='not exists')


if __name__ == '__main__':
    unittest.main()
